package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Module;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.ModuleService;
import com.itheima.utils.UUIDUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class ModuleServiceImpl implements ModuleService {
    @Override
    public PageInfo<Module> queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        PageHelper.startPage(pageNum,pageSize);
        List<Module> list = moduleDao.findAll();
        PageInfo<Module> pageInfo = new PageInfo<>(list);

        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<Module> findAll() throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        List<Module> list = moduleDao.findAll();

        sqlSession.close();
        return list;
    }

    @Override
    public Module findById(String id) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        Module module = moduleDao.findById(id);

        sqlSession.close();
        return module;
    }

    @Override
    public void save(Module module) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        module.setId(UUIDUtil.getSimpleUUID());
        moduleDao.save(module);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void update(Module module) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        moduleDao.update(module);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteById(String... ids) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        for (String id : ids) {
            moduleDao.deleteById(id);
        }

        sqlSession.commit();
        sqlSession.close();
    }
    @Override
    public List<Map> findAuthorByRoleId(String roleId) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        List<Map> list = roleDao.findAuthorByRoleId(roleId);

        sqlSession.close();
        return list;
    }

    @Override
    public List<Map> findModulesByUserId(String userId) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        ModuleDao moduleDao = sqlSession.getMapper(ModuleDao.class);

        List<Map> list = moduleDao.findModulesByUserId(userId);

        sqlSession.close();
        return list;
    }
}
